/* Operations that deal with the customer's account */
$(function() {

    // 字符验证
    jQuery.validator.addMethod("stringCheck", function(value, element) {
        return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
    }, "只能包括中文字、英文字母、数字和下划线");

    // 中文字两个字节
    jQuery.validator.addMethod("byteRangeLength", function(value, element,
        param) {
        var length = value.length;
        for (var i = 0; i < value.length; i++) {
            if (value.charCodeAt(i) > 127) {
                length++;
            }
        }
        return this.optional(element) || (length >= param[0] && length <= param[1]);
    }, "请确保输入的值在3-15个字节之间(一个中文字算2个字节)");

    // 身份证号码的验证规则
    function isIdCardNo(num) { // if (isNaN(num)) {alert("输入的不是数字！"); return
            // false;}
            var len = num.length,
                re;
            if (len == 15)
                re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{2})(\w)$/);
            else if (len == 18)
                re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\w)$/);
            else {
                // alert("输入的数字位数不对。");
                return false;
            }
            var a = num.match(re);
            if (a != null) {
                if (len == 15) {
                    var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
                    var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
                } else {
                    var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
                    var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
                }
                if (!B) {
                    // alert("输入的身份证号 "+ a[0] +" 里出生日期不对。");
                    return false;
                }
            }
            if (!re.test(num)) {
                // alert("身份证最后一位只能是数字和字母。");
                return false;
            }
            return true;
        }
        // 身份证号码验证
    jQuery.validator.addMethod("isIdCard", function(value, element) {
        return this.optional(element) || isIdCardNo(value);
    }, "请正确填写您的身份证号码");

    // 手机号码验证
    jQuery.validator.addMethod("isMobile", function(value, element) {
        var length = value.length;
        var mobile = /^0?1[345678]\d{9}$/;
        return this.optional(element) || (length == 11 && mobile.test(value));
    }, "请正确填写您的手机号码");

    // 邮政编码验证
    jQuery.validator.addMethod("isZipCode", function(value, element) {
        var zc = /^[0-9]{6}$/;
        return this.optional(element) || (zc.test(value));
    }, "请正确填写您的邮政编码");


    // 管理收货地址 新增验证
    $('.manage-account').validate({
        rules: {
            'address.firstName': {
                required: true,
                maxlength: 10
            },
            'address.phonePrimary': {
                required: true,
                isMobile: true,
                digits: true,
            },
            'address.receiver.idCard': {
                required: true,
                isIdCard: true
            },
            'address.addressLine1': {
                required: true,
            },
            'address.city': {
                required: true
            },
            'address.state': {
                required: true
            },
            'address.emailAddress': {
                required: true,
            },
            'address.postalCode': {
                required: true,
                isZipCode: true
            }
        },
        messages: {
            'address.firstName': {
                required: "请输入您的名字",
                maxlength: jQuery.validator.format("输入的名字不要超过 {0} 个字符"),
            },
            'address.phonePrimary': {
                required: "请输入电话.",
                isMobile: "请填写正确的电话号码",
                digits: "请输入数字",
            },
            'address.receiver.idCard': {
                required: "请输入身份证.",
                isIdCard: "请输入正确的身份证号码."
            },
            'address.addressLine1': {
                required: "请输入您的地址."
            },
            'address.city': {
                required: ""
            },
            'address.state': {
                required: "请输入您所在的省或直辖市名."
            },
            'address.emailAddress': {
                required: "请输入正确的邮箱."
            },
            'address.postalCode': {
                required: "请输入邮编",
                isZipCode: "请输入正确的邮编."
            }
        },
        errorPlacement: function(error, element) {
            if ($('body').width() > 480)
                $(error).css("left", $(element).parent().width() + "px");
            $(element).parent().append(error);
        },
        highlight: function(element, errorClass) {
            $(element).addClass('fieldError');
        },
        unhighlight: function(element, errorClass) {
            $(element).removeClass('fieldError');
        },
        errorElement: "span",
        errorClass: "error",
        submitHandler: function(form) {
            form.submit();
        }
    });

    // 验证码按钮

    var wait;

    $('#captcha_btn').on("click",
        function() {
            var url = location.href.substring(8);
            var indexOfSlash = url.indexOf('/');
            var host = url.substring(0, indexOfSlash);
            var mobile = $('#mobile').val();
            
            if(mobile==="" || mobile=== undefined){
            	alert("请输入手机号码")
            	return;
            }
            
            $("#captcha_btn").attr("disabled", "true");
            $.get('https://' + host + '/shop/captcha/sendCaptcha?mobile=' + mobile);
            wait = 60;
            InterValObj = window.setInterval(SetRemainTime, 1000); // 启动计时器，1秒执行一次
        });

    // timer处理函数
    function SetRemainTime() {
        if (wait == 0) {
            window.clearInterval(InterValObj); // 停止计时器
            $("#captcha_btn").removeAttr("disabled"); // 启用按钮
            $("#captcha_btn").html("重新发送验证码");
        } else {
            wait--;
            $("#captcha_btn").html("请在" + wait + "秒内输入验证码");
        }

    }

    $('#updateMobileForm_submit_btn').click(function() {
        document.getElementById('updateMobileForm').submit();
        return false;
    });
    
    $('#mergeForm_submit_btn').click(function() {
        document.getElementById('mergeAccountForm').submit();
        return false;
    });
});
